-- revive
-- created by panyl
-- 死亡复活恢复血量N%

return {
    apply = function(prop, baseValue)
        -- baseValue期望是最大血量，返回的是恢复的血量
        return math.modf(baseValue * prop[3] / 1000);
    end,

    -- 死亡触发
    trigger = function(who, prop, arg)
        -- 如果已經復活過了
        if who.hasRevived then
            return false;
        end

        -- 是玩家的话抛出施放特殊技能事件
        if who.type == OBJECT_TYPE_USER then
            if type(ME.user.dbase:queryTemp("delayHpEffect")) == "number" then
                -- 延迟播放复活特效
                ME.user.dbase:setTemp("delayRevive", true);
            else
                EventMgr.fire(event.HOLY_REVIVE_EFFECT);
            end
        end

        -- 标志已经复活过了
        who.hasRevived = true;

        local p = prop[3];
        local maxHp = who:getMaxHp();
        local hp = math.modf(maxHp * p / 1000);
        local baseHp = who.dbase:query("hp", 0);

        -- 设置血量
        who:addAttrib("hp", hp - who:queryAttrib("hp"), "revive");

        -- 同时恢复魔法
        if who.type == OBJECT_TYPE_USER then
            p = PropM.combine(who, "magic_revive", 45);
            local max_mp = who:getMaxMp();
            local add_mp = PropM.apply(p, max_mp);

            -- 不能超过上限
            local requireMp = max_mp - who:queryAttrib("mp");
            add_mp = math.min(add_mp, requireMp);

            who:addAttrib("mp", add_mp, "revive");

            -- 清除负面状态
            CombatStatusM.reviveClean(who);
        end

        return who:getHp() > 0;
    end,

    desc = function(prop)
        local row = PropM.getRow(prop[1]);
        local desc = row.desc;
        local maxHp = ME.user:getMaxHp();

        local key = FormulaM.invoke("CALC_SKILL_APPEND_PROP", 45);
        if key then
            local p = PropM.combine(ME.user, key, 45);
            if p[3] > 0 then
                desc = row.append_desc;
            end
        end

        local v = math.modf(prop[3] * maxHp / 1000);
        desc = string.gsub(desc, "{1}", v);

        return desc;
    end,

    record = function(target, prop, path)
        return false;
    end
};
